package liangtWorkSpace.simple;

import java.math.BigDecimal;

/**
 * @author liangt
 * @ClassName: PlusOne
 * @title: PlusOne
 * @projectName liangtWorkSpace.simple
 * @description: 数组加一
 * @date 2020/10/22 19:11
 */
public class PlusOne {
    public int[] plusOne(int[] digits) {
        String number = new String();
        for (int i = 0; i < digits.length ; i++){
            number+=digits[i];

        }

        System.out.println(number);
        BigDecimal bigDecimal = new BigDecimal(number);
        number = bigDecimal.add(new BigDecimal(1)).toString();
        int[] ints = new int[number.length()];
        String[] strings = number.split("");
        for (int i = 0;i < strings.length;i++){
            ints[i] = Integer.valueOf(strings[i]);
        }
        return ints;
    }
    public int[] plusOne1(int[] digits) {
        for (int i = digits.length-1; i >= 0; i--){
            //末位不为9的，直接加以返回
            if (digits[i] != 9){
                digits[i] +=1;
                break;
            }
            //末尾为9的
            if (digits[i] == 9){
                digits[i] = 0;
            }

            if (i == 0 && digits[i] == 0 ){
                digits = setNewInt(digits);
                i +=1;
            }
        }
        return digits;
    }

    private  int[] setNewInt(int[] digits){
        int[] ints = new int[digits.length+1];
        for (int i = ints.length-1;i >0 ;i-- ){
            ints[i] = digits[i-1];
        }
        return ints;
    }
    public static void main(String[] args) {
        int[] ints = new int[]{7,2,8,5,0,9,1,2,9,5,3,6,6,7,3,2,8,4,3,7,9,5,7,7,4,7,4,9,4,7,0,1,1,1,7,4,0,0,6};
        ints = new int[]{1,1,9,9,9,9,9,9};
        ints  = new PlusOne().plusOne1(ints);
        for (int i : ints){
            System.out.print(i);
        }
    }
}
